package main

import (
	"./utils"
	"./source"
	"fmt"
	"bytes"
	"strings"
	"time"
	"github.com/PuerkitoBio/goquery"

)

var (
	client       = utils.NewHttpClient()
	contentChain = make(chan analyseChanData)
)

type item struct {
	Name      string
	Aka       string
	Season    string
	Href      string
	UpdateNum string
	Updated   time.Time
}

type analyseChanData struct {
	Buf []byte
	Uri string
}

func request() {
	//buf, err := ioutil.ReadFile("d:\\aaa.html")
	//if err != nil {
	//	fmt.Println("读取视频页面失败", err)
	//	return
	//}
	buf, err := client.Get("http://videos.yizhansou.com")
	if err != nil {
		fmt.Println("加载内容失败", err)
		return
	}
	//fd, _ := os.OpenFile("d:\\aaa.html", os.O_CREATE|os.O_RDWR, 0666)
	//fd.Write(buf)
	//fd.Close()
	v := analyseChanData{
		Buf: buf,
		Uri: "http://videos.yizhansou.com",
	}
	fmt.Println("----------------------------")
	contentChain <- v
}

func analyse() {
	fmt.Println("analyse")
	v := <-contentChain
	doc, err := goquery.NewDocumentFromReader(bytes.NewReader(v.Buf))
	if err != nil {
		fmt.Println("解析页面失败")
		return
	}
	doc.Find("body>table:nth-child(10)").Find("tr:not(:first-child)").Each(func(i int, selection *goquery.Selection) {
		sel := selection.Find("td")
		if sel.Length() == 4 {
			var href, aka, name, season, updateNum, update string
			for i := 0; i < 4; i++ {
				if i < 3 {
					asel := sel.Eq(i).Find("a")
					text := asel.Text()
					if i == 0 {
						tHref, ok := asel.Attr("href")
						if !ok {
							fmt.Println("不存在链接属性")
							return
						}
						href = v.Uri + tHref
						arr := strings.Split(text, " - ")
						aka, name = strings.TrimSpace(arr[0]), strings.TrimSpace(arr[1])
					} else if i == 1 {
						season = text
					} else if i == 2 {
						updateNum = text
					}
				} else {
					update = sel.Eq(i).Text()
				}
			}
			timeLayout := "2006-01-02"
			loc, err := time.LoadLocation("Local")
			if err != nil {
				fmt.Println("获取时区失败", err)
				return
			}
			tm, err := time.ParseInLocation(timeLayout, update, loc)
			if err != nil {
				fmt.Println("解析时间异常", err)
				return
			}
			it := item{
				Name:      name,
				Aka:       aka,
				Season:    season,
				Href:      href,
				UpdateNum: updateNum,
				Updated:   tm,
			}
			fmt.Printf("%+v\n", it)
		}
	})

	fmt.Println("11111")

}

func analyseItem() {
	//buf, err := ioutil.ReadFile("d:\\4849.html")
	//if err != nil {
	//	fmt.Println("读取视频页面失败", err)
	//	return
	//}
	buf, err := client.Get("http://videos.yizhansou.com/4934")
	if err != nil {
		fmt.Println("加载内容失败", err)
		return
	}
	//fd, _ := os.OpenFile("d:\\4849.html", os.O_CREATE|os.O_RDWR, 0666)
	//fd.Write(buf)
	//fd.Close()
	doc, err := goquery.NewDocumentFromReader(bytes.NewReader(buf))
	if err != nil {
		fmt.Println("解析页面失败")
		return
	}
	emules := doc.Find("#d_3_1>table:nth-child(2)")
	emules.Find("table").Each(func(i int, selection *goquery.Selection) {
		videoType := selection.Parent().Prev().Text()
		if strings.TrimSpace(videoType) == "高清" {
			fmt.Println(videoType, "   ", "emule ", "HD")
		} else {
			fmt.Println(videoType, "   ", "emule ", "NM")
		}
		selection.Find("td>a").Each(func(i int, sel *goquery.Selection) {
			href, ok := sel.Attr("href")
			if ok {
				fmt.Println(sel.Text(), "   ", href)
			}
		})
	})
	fmt.Println("##################################")
	thumders := doc.Find("#d_3_3>table:nth-child(2)")
	thumders.Find("table").Each(func(i int, selection *goquery.Selection) {
		videoType := selection.Parent().Prev().Text()
		if strings.TrimSpace(videoType) == "高清" {
			fmt.Println(videoType, "   ", "thumder ","HD")
		} else {
			fmt.Println(videoType, "   ", "thumder ","NM")
		}
		selection.Find("td>a").Each(func(i int, sel *goquery.Selection) {
			href, ok := sel.Attr("href")
			if ok {
				fmt.Println(sel.Text(), "   ", href)
			}
		})
	})
}

func main() {
	//analyseItem()
	//go request()
	//analyse()
	source.NewYzsSource()
}
